//汽水瓶
#include<iostream>
using namespace std;
int caclsum(int n)
{
    int ret=0;
    while(n>1)
    {
        int sum=n/3;
        int left=n%3;
        ret+=sum;
        n=sum+left;
        if(n==2)
        {
            ret++;
            break;
        }
    }
    return ret;
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        
        cout<<caclsum(n)<<endl;
    }
    return 0;
}

//最长子串

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string solo(string& shor,string& len)
{
    int len1=shor.size();
    int len2=len.size();
    int max=0;
    int start=0;
    vector<vector<int>> MSC(len1+1,vector<int>(len2+1,0));
    for(int i=1;i<=len1;++i)
    {
        for(int j=1;j<=len2;++j)
        {
            if(shor[i-1]==len[j-1])
            MSC[i][j]=MSC[i-1][j-1]+1;
            if(MSC[i][j]>max)
            {
                max=MSC[i][j];
                start=i-max;
            }
        }
    }
    return shor.substr(start,max);
}
int main()
{
    string str1;
    string str2;
    while(cin>>str1>>str2)
    {
    if(str1.size()<=str2.size())
        cout<<solo(str1,str2)<<endl;
    else
        cout<<solo(str2,str1)<<endl;
    return 0;
    }
}